package org.example.operator;

import org.example.operator.impl.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class OperatorFactory {
    public static Map<Character, Supplier<Operator>> OPERATOR_MAP=new HashMap<Character, Supplier<Operator>>();

    static {
        OPERATOR_MAP.put('+',AddOperator::new);
        OPERATOR_MAP.put('-',SubOperator::new);
        OPERATOR_MAP.put('*',MulOperator::new);
        OPERATOR_MAP.put('/',DivideOperator::new);
        OPERATOR_MAP.put('^',ExponentOperator::new);
    }

    public static void addOperator(Character sign,Supplier<Operator> supplier){
        OPERATOR_MAP.put(sign,supplier);
    }

    public Operator getOperator(char sign) {//使用简单工厂模式根据运算符创建实现相关运算的对象
        return OPERATOR_MAP.getOrDefault(sign,NoOperator::new).get();
    }
}